package com.swk.version4.behaviorImpl;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.swk.version4.bean.Chapter;
import com.swk.version4.behavior.HandleChapterListBehavior;
import com.swk.version4.util.AnalyzeUtils;

public class HandleListForDingDianxs implements HandleChapterListBehavior {

	@Override
	public List<Chapter> handleChapterList(Document doc, String analyzeParam) {
		//返回结果
		List<Chapter> chapterList = new ArrayList<Chapter>();
		
		//<td class="L"><a href="15984402.html">第二十一章 定神符</a></td>
		//根据标签获取章节列表
		Elements list = doc.getElementsByTag("a");
		
		for (Element element : list) {
			String chapterName = element.text().replace(Jsoup.parse("&nbsp;").text(), " ");//章节名
			chapterName = AnalyzeUtils.addZhang(chapterName);//添加第、章等
			String chapterHref = element.attr("href");//链接
			
			//根据网页情况，筛选需要的
			//https://www.x23us.com/html/0/136/
			//https://www.x23us.com/html/0/146/
			//https://www.x23us.com/html/0/146/15985495.html
			if("15984402.html".length() == chapterHref.length()){
				Chapter chapter = new Chapter();
				chapter.setChapterName(chapterName);
				chapter.setChapterHref("https://www.x23us.com/html/0/" + analyzeParam + chapterHref);
				chapterList.add(chapter);
			}
		}
		if(chapterList == null || chapterList.size() == 0){
			System.out.println("解析章节列表失败！");
			throw new RuntimeException("解析章节列表失败！");
		}
		return chapterList;
	}

}
